package com.time.hellotime.views;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;

import com.time.hellotime.R;


/**
 * 手机验证码自定义布局
 * Author: Created by deng on 2016/8/18.
 * E-mail: tx16bing@163.com
 */
public class CodeView extends FrameLayout {

    private EditText etCode;
    private String hintText;

    public CodeView(Context context) {
        super(context);
    }

    public CodeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View view = LayoutInflater.from(getContext())
                .inflate(R.layout.item_code, this, false);
        addView(view);
        initView(view);
    }

    private void initView(final View view) {
//        final ImageView ivIcon = view.findViewById(R.id.iv_icon);
        etCode = view.findViewById(R.id.et_code);

        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (etCode.getText().length() == 0) {
//                    ivIcon.setImageResource(R.drawable.iv_register_code_unpressed);
                } else {
//                    ivIcon.setImageResource(R.drawable.iv_register_code_pressed);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        etCode.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
//                    ivIcon.setImageResource(R.drawable.iv_register_code_pressed);
                } else {
//                    ivIcon.setImageResource(R.drawable.iv_register_code_unpressed);
                }
            }
        });
    }

    public String getCode() {
        return etCode.getText().toString().trim();
    }

}
